package com.web.music.impl;

import com.web.music.common.MusicException;
import com.web.music.interfaces.XmlParser;

/**
 * The common part for all XML parsers.
 */
public abstract class BaseXmlParser implements XmlParser {

	static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
	
	/**
	 * Property to indicate if XML validation against XML Schema should be performed
	 */
	private boolean validateXML;
	
	public boolean isValidateXML() {
		return validateXML;
	}

	public void setValidateXML(boolean validateXML) {
		this.validateXML = validateXML;
	}
	
	/**
	 * Id values in XML file are in a form: [id_prefix][long value].
	 * Extracts id long value from full string.
	 * @param nodeValue - full id string
	 * @param prefix - id prefix
	 * @return - id Long value to be saved in DB
	 */
	protected Long extractIdValue(String fullIdValue, String prefix) {
		if (fullIdValue == null) return null;
		if (fullIdValue.startsWith(prefix)){
			String idValue = fullIdValue.substring(prefix.length(), fullIdValue.length());
			try {
				return Long.parseLong(idValue);
			} catch (NumberFormatException e) {
				throw new MusicException("Wrong ID format: " + fullIdValue);
			}
		}
		else{
			throw new MusicException("Wrong ID format: " + fullIdValue);
		}
	}
}
